//
//  NEDBaseNavigationController.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/9/20.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "NEDBaseNavigationController.h"
#import "UIViewController+NEDBase.h"
#import "UINavigationBar+NED.h"
//#import <KMNavigationBarTransition/UIViewController+KMNavigationBarTransition_internal.h>
#import "UIViewController+NEDBase.h"
#import "NEDConstant.h"

@interface NEDBaseNavigationController ()<UINavigationControllerDelegate>

/** 是否正在pushing */
@property (assign, nonatomic) BOOL pushing;

@end

@implementation NEDBaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.delegate = self;
    
    [self.navigationBar ned_setNavigationBar];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    UIViewController* topVC = self.topViewController;
    return [topVC preferredStatusBarStyle];
}


//-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
//    [super traitCollectionDidChange:previousTraitCollection];
//        
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self setNeedsStatusBarAppearanceUpdate];
//
//        [self.navigationBar ned_setNavigationBar];
//        [self.navigationBar layoutSubviews];
//
//        for(UIViewController * vc in self.navigationController.childViewControllers){
//            [vc.km_transitionNavigationBar ned_setNavigationBar];
//        }
//        
//        for(UIViewController * vc in self.childViewControllers){
//            if([vc conformsToProtocol:@protocol(NEDBaseViewControllerDelegate)]){
//                [vc.km_transitionNavigationBar ned_setNavigationBar];
//                [vc ned_navigationBarShowStyleUpdate];
//            }
//        }
//    });
//}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 用于阻止重复push
    if (self.pushing){
        return;
    }
    self.pushing = YES;

    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }else{
        viewController.hidesBottomBarWhenPushed = NO;
    }
    
    [super pushViewController:viewController animated:animated];
}

-(NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated{
    if (animated) {
        UIViewController *popController = self.viewControllers.lastObject;
        popController.hidesBottomBarWhenPushed = NO;
    }
    NSArray * vcs = [super popToRootViewControllerAnimated:animated];
    return vcs;
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.pushing = NO;
    
    NSLog(@"👁 show: %@",NSStringFromClass(viewController.class));
}

- (BOOL)willDealloc{
    return NO;
}



@end
